package com.hb.trans.mvp.presenter

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.OnLifecycleEvent
import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.hb.trans.bean.AboutUs
import com.hb.trans.bean.Base
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.AboutUsContract
import com.hb.trans.mvp.model.AboutUsModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class AboutUsPresenter @Inject constructor(model: AboutUsModel, var errorHandler: RxErrorHandler) :
    BasePresenter<AboutUsContract.View, AboutUsContract.Model>(model) {

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun getAboutUs() {
        mModel.getAboutUsDescription()
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<AboutUs>>(errorHandler) {
                override fun next(r: Base<AboutUs>) {
                    if (r.code == 1) {
                        mRootView?.setupAboutUsDescription(r.data)
                    } else {
                        mRootView?.showShortToast(r.info ?: "")
                    }
                }
            })
    }
}